package tzhomestudio.framework.implementation.log_service;

import tzhomestudio.framework.implementation.main_service.FrameworkMainServiceHelper;
import tzhomestudio.framework.interfaces.log_service.IWindowMessenger;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 * This class contains methods for output of
 * different information on the window.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class WindowMessager implements IWindowMessenger {
    @Override
	public void errorMessage(final String text, final String title) {
        FrameworkMainServiceHelper.logger.printError(text);
		if (SwingUtilities.isEventDispatchThread()) {
			String msg;
			if(text != null) {
				msg = text + "\n\nAdditional information : \n" + FrameworkMainServiceHelper.logger.getLoggerInfo();
			} else {
				msg = "Additional information : \n" + FrameworkMainServiceHelper.logger.getLoggerInfo();
			}
			Object[] options = {"Close"};
			JOptionPane.showOptionDialog(null,msg,title,JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE,null,options,options[0]);
		} else {
			SwingUtilities.invokeLater(new Runnable() {			
				public void run() {
					errorMessage(text, title);
				}
			});
		}
	}

    @Override
    public void warningMessage(final String text, final String title) {
        FrameworkMainServiceHelper.logger.printError(text);
		if (SwingUtilities.isEventDispatchThread()) {
			String msg;
			if(text != null) {
				msg = text + "\n\nAdditional information : \n" + FrameworkMainServiceHelper.logger.getLoggerInfo();
			} else {
				msg = "Additional information : \n" + FrameworkMainServiceHelper.logger.getLoggerInfo();
			}
			Object[] options = {"Close"};
			JOptionPane.showOptionDialog(null,msg,title,JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
		} else {
			SwingUtilities.invokeLater(new Runnable() {			
				public void run() {
					warningMessage(text, title);
				}
			});
		}
	}

    @Override
    public void infoMessage(final String text, final String title) {
        FrameworkMainServiceHelper.logger.printInfo(text);
		if (SwingUtilities.isEventDispatchThread()) {
			Object[] options = {"Close"};
			JOptionPane.showOptionDialog(null,text,title,JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,options,options[0]);
		} else {
			SwingUtilities.invokeLater(new Runnable() {			
				public void run() {
					infoMessage(text, title);
				}
			});
		}
	}
}
